package com.shop.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.shop.dao.GoodsDao;
import com.shop.domain.Comment;
import com.shop.domain.Goods;
import com.shop.util.MyBatisUtil;

/**
 * 
 * @author wenjuanhenxing
 * @version V1.0 GoodsDaoImpl.java 2016-2-10 下午7:41:26
 */
@Repository("goodsDaoImpl")
public class GoodsDaoImpl implements GoodsDao {

	@Override
	public Goods getGoodsById(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		Goods goods = sqlSession.selectOne("com.shop.dao.mapping.GoodsMapper"
				+ ".getGoodsById", id);
		MyBatisUtil.closeSession(sqlSession);
		return goods;
	}

	@Override
	public List<Goods> getAllGoods() {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Goods> allGoods = sqlSession.selectList(Goods.class.getName()
				+ "Mapper" + ".getAllGoods");
		MyBatisUtil.closeSession(sqlSession);
		return allGoods;
	}

	@Override
	public List<Goods> getGoodsByLargeCategory(Integer largeCategory) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Goods> goodsOfLargeCategory = sqlSession
				.selectList("com.shop.dao.mapping.GoodsMapper"
						+ ".getGoodsByLargeCategory", largeCategory);
		MyBatisUtil.closeSession(sqlSession);
		return goodsOfLargeCategory;
	}

	@Override
	public List<Goods> getGoodsByCategoryId(Integer categoryId) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Goods> goodsOfLargeCategory = sqlSession.selectList(
				"com.shop.dao.mapping.GoodsMapper" + ".getGoodsByCategoryId",
				categoryId);
		MyBatisUtil.closeSession(sqlSession);
		return goodsOfLargeCategory;
	}

	@Override
	public List<Goods> getGoodsBySmallCategory(Integer smallCategory) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Goods> goodsOfSmallCategory = sqlSession
				.selectList("com.shop.dao.mapping.GoodsMapper"
						+ ".getGoodsBySmallCategory", smallCategory);
		MyBatisUtil.closeSession(sqlSession);
		return goodsOfSmallCategory;
	}

	@Override
	public List<Goods> getGoodsByPrice(double low, double high) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("low", low);
		param.put("high", high);
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Goods> goodsOfPriceInterval = sqlSession.selectList(
				"com.shop.dao.mapping.GoodsMapper" + ".getGoodsByPrice", param);
		MyBatisUtil.closeSession(sqlSession);
		return goodsOfPriceInterval;
	}

	@Override
	public List<Comment> getCommentsByGoodsId(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Comment> goodsComments = sqlSession.selectList(
				"com.shop.dao.mapping.GoodsMapper" + ".getCommentsByGoodsId",
				id);
		MyBatisUtil.closeSession(sqlSession);
		return goodsComments;
	}

	@Override
	public boolean updateName(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateName", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateLargeCategory(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateLargeCategory", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateSmallCategory(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateSmallCategory", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updatePrice(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updatePrice", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateDegree(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateDegree", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateStock(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateStock", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateRecentUpdate(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateRecentUpdate", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateOnlineStatus(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateOnlineStatus", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateCollectCnt(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateCollectCnt", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateTransactionMode(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		String stmt = "com.shop.dao.mapping.GoodsMapper"
				+ ".updateTransactionModeFalse";
		if (goods.getTransactionMode()) {
			stmt = "com.shop.dao.mapping.GoodsMapper"
					+ ".updateTransactionModeTrue";
		}
		int rowsAffected = sqlSession.update(stmt, goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateTransactionAddress(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateTransactionAddress", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateDescription(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateDescription", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean addGoods(Goods goods) {
		if (goods == null) {
			return false;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		String stmt = "com.shop.dao.mapping.GoodsMapper" + ".addGoodsOnline";
		if (!goods.getTransactionMode()) {
			stmt = "com.shop.dao.mapping.GoodsMapper" + ".addGoodsOffline";
		}
		int rowsAffected = sqlSession.insert(stmt, goods);

		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public List<Goods> getGoodsByPage(Integer startRow, Integer rowsPerPage) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startRow", startRow);
		param.put("rowsPerPage", rowsPerPage);
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Goods> goods = sqlSession.selectList(
				"com.shop.dao.mapping.GoodsMapper" + ".getGoodsByPage", param);
		MyBatisUtil.closeSession(sqlSession);
		return goods;
	}

	@Override
	public boolean updateImage(Goods goods) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.GoodsMapper"
				+ ".updateImage", goods);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;

	}

	@Override
	public List<Goods> getGoodsByNameLike(String name) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Goods> goodsOfLargeCategory = sqlSession.selectList(
				"com.shop.dao.mapping.GoodsMapper" + ".getGoodsByNameLike",
				name);
		MyBatisUtil.closeSession(sqlSession);
		return goodsOfLargeCategory;
	}

}
